package com.zhiyou.app.shop.website.service.impl;

import java.util.List;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.common.util.ShopConstants;
import com.zhiyou.app.shop.website.dao.IMsgDao;
import com.zhiyou.app.shop.website.dao.IUserInfoDao;
import com.zhiyou.app.shop.website.dao.pojo.Msg;
import com.zhiyou.app.shop.website.dao.pojo.User;
import com.zhiyou.app.shop.website.service.IMsgService;

@Service(value="msgService")
public class MsgServiceImpl implements IMsgService {

	@Autowired
	private IMsgDao msgDao;
	@Autowired
	private IUserInfoDao userInfoDao;
	//所有邮件
	public void findReceiveMsg(Page page, String userId) {
		Msg msg = new Msg();
		msg.setReceiveId(userId);
		msg.setFlag(ShopConstants.TRUE);
		msgDao.findByExample(page, msg, Order.desc("createDate"));
	}
	//投诉邮件
	public void findComplain(Page page, String userId){
		Msg msg = new Msg();
		msg.setReceiveId(userId);
		msg.setFlag(ShopConstants.TRUE);
		msg.setType("2");  //投诉
		msgDao.findByExample(page, msg, Order.desc("createDate"));
	}

	public void findsendMsg(Page page, String userId) {
		Msg msg = new Msg();
		msg.setSendId(userId);
		msg.setFlag(ShopConstants.TRUE);
		msgDao.findByExample(page, msg, Order.desc("createDate"));
	}

	public void sendMsg(Msg msg) {
		if(msg.getReceiveId() == null || "".equals(msg.getReceiveId())){
			List<User> userList = userInfoDao.findByProperty("role", ShopConstants.ADMIN);
			if(userList != null && userList.size() > 0){
				User user = userList.get(0);
				msg.setReceiveId(user.getUserAccount());
			}
		}
		msgDao.save(msg);
	}

	public Msg findById(String id) {
		return msgDao.findById(id);
	}

	public void save(Msg msg) {
		msgDao.save(msg);
		
	}

	public void update(Msg msg) {
		msgDao.update(msg);
	}
	public void findNoreply(Page page, String userId) {
		Msg msg = new Msg();
		msg.setReceiveId(userId);
		msg.setFlag(ShopConstants.TRUE);
		msg.setReplyId("");
		msgDao.findByExample(page, msg, Order.desc("createDate"));
	}
	public List<Msg> findReply(String msgId) {
		List<Msg> list = msgDao.findByProperty("replyId", msgId);
		return list;
	}
}
